#include "net_uart.h"
#include "main_windw.h"
#include "data_filter/hardware_recv_send.h"
#include "data_filter/Usart1_Process.h"
#include "QDateTime"
#include <QMainWindow>
#include <ui_main_windw.h>


void  Main_windw::Uart_to_net(QByteArray data,qint16 len)
{
    static int mes_num = 0;

    if(init_param.Filter_log_enable)
    {
        for(int i = 0; i < len;i++)
        {
            HAL_UART_RxCpltCallback((uint8_t)data[i]);
        }
        emit Data_parse_signal(1);

    }
    else
    {
        if(!data.isEmpty())
        {
            //得到命令数据
            if(init_param.run_mode == E_RUN_MODE_CLIENT)
            {
                if(client){
                    client->SendData(data);
                }
            }
            else if(init_param.run_mode == E_RUN_MODE_SERVER)
            {
                if(my_server){
                    my_server->SendData(data);
                }
            }
//            qDebug()<<"4";

            if(mes_num++ >= 100)
            {
                ui->textBrowser_net_send->clear();
                mes_num = 0;
            }

            if(init_param.hex_mode == 1)
            {
                QDateTime CurrentTime = QDateTime::currentDateTime();

                QString time  = "<font color=\"#20FF20\">[";
                time.append(CurrentTime.toString("HH:mm:ss"));
                time.append("]  </font>");
                time.append(data.toHex().toUpper());

                ui->textBrowser_net_send->append(time); //放进文本框
            }else{
                ui->textBrowser_net_send->append(data); //放进文本框
            }
//            qDebug()<<"order_get";
        }
    }
}

void Main_windw::Net_to_uart(QByteArray data)
{
    static int mes_num = 0;

    if(Serial_open_state)
    {
        my_serial->Write(data,data.length());  //串口发送数据
    }else{
        check_serial_state();
    }

    if(mes_num++ >= 100)
    {
        ui->textBrowser_net_recv->clear();
        mes_num = 0;
    }

    if(init_param.hex_mode_recv == 1)
    {
        QDateTime CurrentTime = QDateTime::currentDateTime();

        QString time  = "<font color=\"#FF2020\">[";
        time.append(CurrentTime.toString("HH:mm:ss"));
        time.append("]  </font>");
        time.append(data.toHex().toUpper());
        ui->textBrowser_net_recv->append(time);
    }else{
        ui->textBrowser_net_recv->append(data); //放进文本框
    }


    if(Filtershow != nullptr)
    {
        if(Filtershow->get_checkBox_parse_recv_state())
        {
            for(int i = 0; i < data.length();i++)
            {
                HAL_UART_RxCpltCallback((uint8_t)data[i]);
            }
            emit Data_parse_signal(0);
        }
    }

}








